package controller

import (
	"cs-backend/db"
	"cs-backend/dto"
	"cs-backend/model"
	"cs-backend/service"
	"cs-backend/util"
	"github.com/go-playground/validator/v10"
	"github.com/kataras/iris/v12"
)

type UserController struct {
	userService   *service.UserService
	commonService *service.CommonService
}

func (c *UserController) GetById(ctx iris.Context) {
	id, err := ctx.Params().GetInt64("id")
	if err != nil {
		_, _ = ctx.JSON(dto.Response(iris.StatusBadRequest, "param[id] is invalid."))
		return
	}
	//user := new(model.User)
	//user.Base.Id = id
	//return s.BaseService.GetById(user)
	//_, _ = ctx.JSON(c.userService.BaseService.GetById(user))
	_, _ = ctx.JSON(c.userService.GetById(id))
	return
}

func (c *UserController) Insert(ctx iris.Context) {
	var user model.User
	err := ctx.ReadJSON(&user)
	if err != nil {
		// Handle the error, below you will find the right way to do that...
		if errs, ok := err.(validator.ValidationErrors); ok {
			validationErrors := util.WrapValidationErrors(errs)
			_, _ = ctx.JSON(dto.ResponseDataInvalid(validationErrors))
			return
		}

		//ctx.StopWithStatus(iris.StatusInternalServerError)
		_, _ = ctx.JSON(dto.ResponseInternalServerError())
		return
	}
	user.CreatedBy = ctx.Values().GetInt64Default("userid", 0)

	//_, _ = ctx.JSON(c.userService.Insert(&user))
	_, _ = ctx.JSON(c.commonService.Insert(&user))
}

func (c *UserController) Update(ctx iris.Context) {
	var user model.User
	err := ctx.ReadJSON(&user)
	if err != nil {
		// Handle the error, below you will find the right way to do that...
		if errs, ok := err.(validator.ValidationErrors); ok {
			validationErrors := util.WrapValidationErrors(errs)
			_, _ = ctx.JSON(dto.ResponseDataInvalid(validationErrors))
			return
		}

		//ctx.StopWithStatus(iris.StatusInternalServerError)
		_, _ = ctx.JSON(dto.ResponseInternalServerError())
		return
	}
	user.UpdatedBy = ctx.Values().GetInt64Default("userid", 0)
	//_, _ = ctx.JSON(c.userService.Update(&user))
	_, _ = ctx.JSON(c.commonService.Update(&user))
	return
}

func (c *UserController) DeleteById(ctx iris.Context) {
	id, err := ctx.Params().GetInt64("id")
	if err != nil {
		_, _ = ctx.JSON(dto.ResponseBadRequest("参数[id]错误"))
		return
	}
	userid := ctx.Values().GetInt64Default("userid", 0)
	//_, _ = ctx.JSON(c.userService.DeleteById(id, userid))
	user := new(model.User)
	user.Id = id
	_, _ = ctx.JSON(c.commonService.Delete(user, userid))
	return
}

func (c *UserController) ListAll(ctx iris.Context) {
	slice := make([]model.User, 0)
	err := db.GetEngine().Find(&slice)
	if err != nil {
		_, _ = ctx.JSON(dto.ResponseInternalServerErrorAndMsg(err.Error()))
		return
	}
	_, _ = ctx.JSON(slice)
	return
}

func (c *UserController) ListPage(ctx iris.Context) {
	slice := make([]model.User, 0)
	err := db.GetEngine().Find(&slice)
	if err != nil {
		_, _ = ctx.JSON(dto.ResponseInternalServerError())
		return
	}
	_, _ = ctx.JSON(slice)
	return
}

func (c *UserController) SqlMapQuery(ctx iris.Context) {
	_, _ = ctx.JSON(c.userService.ListPage())
	return
}
